package com.ruoyi.wb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wb.domain.WbContract;
import com.ruoyi.wb.domain.WbContractUser;
import com.ruoyi.wb.domain.WbMaintainCompany;
import com.ruoyi.wb.dto.WbContractDto;
import com.ruoyi.wb.enmus.WbContractType;
import com.ruoyi.wb.mapper.WbContractMapper;
import com.ruoyi.wb.mapper.WbContractUserMapper;
import com.ruoyi.wb.service.IWbContractService;
import com.ruoyi.wb.service.IWbMaintainCompanyService;
import com.ruoyi.wb.vo.ClientMessageVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * 维保合同Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-14
 */
@Service
@AllArgsConstructor
public class WbContractServiceImpl implements IWbContractService 
{
    private final WbContractMapper wbContractMapper;
    private final WbContractUserMapper wbContractUserMapper;
    private final IWbMaintainCompanyService iWbMaintainCompanyService;
    /**
     * 查询维保合同
     * 
     * @param id 维保合同主键
     * @return 维保合同
     */
    @Override
    public WbContract selectWbContractById(Long id)
    {
        return wbContractMapper.selectWbContractById(id);
    }

    /**
     * 查询维保合同列表
     * 
     * @param wbContract 维保合同
     * @return 维保合同
     */
    @Override
    public List<WbContract> selectWbContractList(WbContract wbContract)
    {

        //修改维保合同 是否生效
        wbContractMapper.updateWbContractStateValid(WbContractType.VALID.getCode(),WbContractType.INVALID.getCode(),WbContractType.TERMINATION.getCode());
        wbContractMapper.updateWbContractStateInvalid(WbContractType.VALID.getCode(),WbContractType.INVALID.getCode(),WbContractType.TERMINATION.getCode());

        SysUser user  = SecurityUtils.getLoginUser().getUser();
        //获取自己维保公司 如果有维保公司则只能查看维保公司数据
        List<WbMaintainCompany> wbMaintainCompanies = iWbMaintainCompanyService.selectWbMaintainCompanyByUserId(user.getUserId());
        if (!CollectionUtils.isEmpty(wbMaintainCompanies)){
            wbContract.setWbCompanyId(wbMaintainCompanies.get(0).getId());
        }
        if (!ObjectUtils.isEmpty(wbContract)&&!ObjectUtils.isEmpty(wbContract.getEndTime())){
            Date endTime = wbContract.getEndTime();
            endTime.setHours(23);
            endTime.setMinutes(59);
            endTime.setSeconds(59);
            wbContract.setEndTime(endTime);
        }
        PageUtils.startPage();
        return wbContractMapper.selectWbContractList(wbContract);
    }

    /**
     * 新增维保合同
     * 
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWbContract(WbContractDto wbContractDto)
    {

        //确保合同编号唯一
        List<WbContract> wbContractList = wbContractMapper.selectWbContractListByCode(wbContractDto.getCode());
        if (!CollectionUtils.isEmpty(wbContractList)){
            throw new RuntimeException("合同编号已存在,请重新输入");
        }

        if (ObjectUtils.isEmpty(wbContractDto)){
            return 0;
        }
        WbContract wbContract = BeanUtil.copyProperties(wbContractDto, WbContract.class);

        wbContract.setCreateTime(DateUtils.getNowDate());
        Date contractValidityStart = wbContract.getContractValidityStart();
        contractValidityStart.setHours(0);
        contractValidityStart.setMinutes(0);
        contractValidityStart.setSeconds(0);
        wbContract.setContractValidityStart(contractValidityStart);
        Date contractValidityEnd = wbContract.getContractValidityEnd();
        contractValidityEnd.setHours(23);
        contractValidityEnd.setMinutes(59);
        contractValidityEnd.setSeconds(59);
        wbContract.setContractValidityEnd(contractValidityEnd);
        Date itemMaintenanceValidityStart = wbContract.getItemMaintenanceValidityStart();
        itemMaintenanceValidityStart.setHours(0);
        itemMaintenanceValidityStart.setMinutes(0);
        itemMaintenanceValidityStart.setSeconds(0);
        wbContract.setItemMaintenanceValidityStart(itemMaintenanceValidityStart);
        Date itemMaintenanceValidityEnd = wbContract.getItemMaintenanceValidityEnd();
        itemMaintenanceValidityEnd.setHours(23);
        itemMaintenanceValidityEnd.setMinutes(59);
        itemMaintenanceValidityEnd.setSeconds(59);
        wbContract.setItemMaintenanceValidityEnd(itemMaintenanceValidityEnd);
        //新增合同
        int i = wbContractMapper.insertWbContract(wbContract);
        if (i==0){
         throw new RuntimeException("新增合同失败");
        }

        //新增合同用户关联表
        List<WbContractUser> wbContractUsers = wbContractDto.getWbContractUsers();
        wbContractUsers.forEach(item->{
            item.setContractId(wbContract.getId());
        });
        if (CollectionUtils.isEmpty(wbContractUsers)){
            return i;
        }
        int j=wbContractUserMapper.insertWbContractUsers(wbContractUsers);

        return i;
    }

    /**
     * 修改维保合同
     * 
     * @param wbContract 维保合同
     * @return 结果
     */
    @Override
    public int updateWbContract(WbContract wbContract)
    {
        wbContract.setUpdateTime(DateUtils.getNowDate());
        return wbContractMapper.updateWbContract(wbContract);
    }

    /**
     * 批量删除维保合同
     * 
     * @param ids 需要删除的维保合同主键
     * @return 结果
     */
    @Override
    public int deleteWbContractByIds(Long[] ids)
    {
        return wbContractMapper.deleteWbContractByIds(ids);
    }

    /**
     * 删除维保合同信息
     * 
     * @param id 维保合同主键
     * @return 结果
     */
    @Override
    public int deleteWbContractById(Long id)
    {
        return wbContractMapper.deleteWbContractById(id);
    }

    /**
     * 查询合同信息包含维修记录
     * @param clientId
     * @return
     */
    @Override
    public ClientMessageVo getContractInfoByClientId(Long clientId) {
        return wbContractMapper.getContractInfoByClientId(clientId);
    }
}
